package com.ttg.utils;

//~--- JDK imports ------------------------------------------------------------

import java.util.regex.Pattern;

//~--- classes ----------------------------------------------------------------

/**
 * Class Assert
 * Description
 * Create 2016-02-25 00:38:37 
 * @author Ardy    
 */
public abstract class Assert {

    /** 
     * Field COUNT_RECORD_PATTERN
     * Description 
     */
    private static final String COUNT_RECORD_PATTERN = "(?!0)\\d{1,8}";

    /** 
     * Field BANK_CARD_TYPE_PATTERN
     * Description 
     */
    private static final String BANK_CARD_TYPE_PATTERN = "0|1|2|[\\s]{0}";

    /** 
     * Field BANK_TYPE_PATTERN
     * Description 
     */
    private static final String BANK_TYPE_PATTERN = "[0-9]{7}";

    /** 
     * Field CUST_CERTIFICATION_TYPE
     * Description 
     */
    private static final String CUST_CERTIFICATION_TYPE = "[0-9A-Z]{1}|[\\s]{0}";

    /** 
     * Field AMOUNT_PATTERN
     * Description 
     */
    private static final String AMOUNT_PATTERN = "(?!0)\\d{1,15}";

    /** 
     * Field BANK_ACCT_TYPE
     * Description 
     */
    private static final String BANK_ACCT_TYPE = "1|2|[\\s]{0}";

    /** 
     * Field CARD_VALID_DATE
     * Description 
     */
    private static final String CARD_VALID_DATE = "([0][1-9]|[1][0-2])[0-9]{2}|[\\s]{0}";

    /** 
     * Field CARD_CVV2
     * Description 
     */
    private static final String CARD_CVV2 = "[0-9]{3}|[\\s]{0}";

    /** 
     * Field BANK_ACCOUNT_NO
     * Description 
     */
    private static final String BANK_ACCOUNT_NO = "[0-9-]{8,32}";

    /** 
     * Field ONE_OR_TWO_REQUIRED
     * Description 
     */
    private static final String ONE_OR_TWO_REQUIRED = "[1,2]{1}";

    /** 
     * Field CURRENCY_REGEXP
     * Description 
     */
    private static final String CURRENCY_REGEXP = "([CNY]|[USD]|[EUR]|[HKD]){3}";

    /**
     * Constructs Assert
     * Description
     */
    public Assert() {}

    /**
     * Method isTrue 
     * Description 说明：
     *
     * @param expression 类型：boolean
     * @param message 类型：String
     */
    public static void isTrue(boolean expression, String message) {
        if (!expression) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Method isTrue 
     * Description 说明：
     *
     * @param expression 类型：boolean
     */
    public static void isTrue(boolean expression) {
        isTrue(expression, "[Assertion failed] - this expression must be true");
    }

    /**
     * Method isNull 
     * Description 说明：
     *
     * @param object 类型：Object
     * @param message 类型：String
     */
    public static void isNull(Object object, String message) {
        if (object != null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Method isNull 
     * Description 说明：
     *
     * @param object 类型：Object
     */
    public static void isNull(Object object) {
        isNull(object, "[Assertion failed] - the object argument must be null");
    }

    /**
     * Method notNull 
     * Description 说明：
     *
     * @param object 类型：Object
     * @param message 类型：String
     */
    public static void notNull(Object object, String message) {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Method notNull 
     * Description 说明：
     *
     * @param object 类型：Object
     */
    public static void notNull(Object object) {
        notNull(object, "[Assertion failed] - this argument is required; it must not be null");
    }

    /**
     * Method hasLength 
     * Description 说明：
     *
     * @param text 类型：String
     * @param length 类型：int
     * @param message 类型：String
     */
    public static void hasLength(String text, int length, String message) {
        if (!hasLength((CharSequence) text, length)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Method hasLength 
     * Description 说明：
     *
     * @param text 类型：String
     */
    public static void hasLength(String text) {
        hasLength(text, 1, "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }

    /**
     * Method hasLength 
     * Description 说明：
     *
     * @param text 类型：String
     * @param length 类型：int
     */
    public static void hasLength(String text, int length) {
        hasLength(text, length,
                  "[Assertion failed] - this String argument must have length; it must not be null or empty");
    }

    /**
     * Method hasText 
     * Description 说明：
     *
     * @param text 类型：String
     * @param message 类型：String
     */
    public static void hasText(String text, String message) {
        if (!hasText((CharSequence) text)) {
            throw new IllegalArgumentException(message);
        }
    }

    /**
     * Method hasText 
     * Description 说明：
     *
     * @param text 类型：String
     */
    public static void hasText(String text) {
        hasText(text,
                "[Assertion failed] - this String argument[" + text
                + "] must have text; it must not be null, empty, or blank");
    }

    /**
     * Method noNullElements 
     * Description 说明：
     *
     * @param array 类型：Object[]
     * @param message 类型：String
     */
    public static void noNullElements(Object[] array, String message) {
        if (array != null) {
            for (int i = 0 ; i < array.length ; ++i) {
                if (array[i] == null) {
                    throw new IllegalArgumentException(message);
                }
            }
        }
    }

    /**
     * Method noNullElements 
     * Description 说明：
     *
     * @param array 类型：Object[]
     */
    public static void noNullElements(Object[] array) {
        noNullElements(array, "[Assertion failed] - this array must not contain any null elements");
    }

    /**
     * Method isInstanceOf 
     * Description 说明：
     *
     * @param clazz 类型：Class
     * @param obj 类型：Object
     */
    public static void isInstanceOf(Class clazz, Object obj) {
        isInstanceOf(clazz, obj, "");
    }

    /**
     * Method isInstanceOf 
     * Description 说明：
     *
     * @param type 类型：Class
     * @param obj 类型：Object
     * @param message 类型：String
     */
    public static void isInstanceOf(Class type, Object obj, String message) {
        notNull(type, "Type to check against must not be null");

        if (!type.isInstance(obj)) {
            throw new IllegalArgumentException(message + "Object of class ["
                                               + ( ( obj != null )
                                                   ? obj.getClass().getName() : "null" ) + "] must be an instance of "
                                                       + type);
        }
    }

    /**
     * Method isAssignable 
     * Description 说明：
     *
     * @param superType 类型：Class
     * @param subType 类型：Class
     */
    public static void isAssignable(Class superType, Class subType) {
        isAssignable(superType, subType, "");
    }

    /**
     * Method isAssignable 
     * Description 说明：
     *
     * @param superType 类型：Class
     * @param subType 类型：Class
     * @param message 类型：String
     */
    public static void isAssignable(Class superType, Class subType, String message) {
        notNull(superType, "Type to check against must not be null");

        if (( subType == null ) ||!superType.isAssignableFrom(subType)) {
            throw new IllegalArgumentException(message + subType + " is not assignable to " + superType);
        }
    }

    /**
     * Method state 
     * Description 说明：
     *
     * @param expression 类型：boolean
     * @param message 类型：String
     */
    public static void state(boolean expression, String message) {
        if (!expression) {
            throw new IllegalStateException(message);
        }
    }

    /**
     * Method state 
     * Description 说明：
     *
     * @param expression 类型：boolean
     */
    public static void state(boolean expression) {
        state(expression, "[Assertion failed] - this state invariant must be true");
    }

    /**
     * Method hasText 
     * Description 说明：
     *
     * @param str 类型：CharSequence
     *
     * @return 返回值说明：
     */
    public static boolean hasText(CharSequence str) {
        if (!hasLength(str, 1)) {
            return false;
        } else {
            int strLen = str.length();

            for (int i = 0 ; i < strLen ; ++i) {
                if (!Character.isWhitespace(str.charAt(i))) {
                    return true;
                }
            }

            return false;
        }
    }

    /**
     * Method hasLength 
     * Description 说明：
     *
     * @param str 类型：CharSequence
     * @param length 类型：int
     *
     * @return 返回值说明：
     */
    public static boolean hasLength(CharSequence str, int length) {
        return ( str != null ) && ( str.length() >= length );
    }

    /**
     * Method hasMaxLength 
     * Description 说明：
     *
     * @param str 类型：String
     * @param maxLength 类型：int
     * @param message 类型：String
     */
    public static void hasMaxLength(String str, int maxLength, String message) {
        if (( null == str ) || "".equals(str.trim()) ||!hasMaxLength(str, maxLength)) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 不能为空,且长度不能超过" + maxLength);
        }
    }

    /**
     * Method hasMaxLength 
     * Description 说明：
     *
     * @param str 类型：CharSequence
     * @param maxLength 类型：int
     *
     * @return 返回值说明：
     */
    public static boolean hasMaxLength(CharSequence str, int maxLength) {
        return ( str != null ) && ( str.length() > 0 ) && ( str.length() <= maxLength );
    }

    /**
     * Method isMatch 
     * Description 说明：
     *
     * @param str 类型：String
     * @param regex 类型：String
     *
     * @return 返回值说明：
     */
    public static boolean isMatch(String str, String regex) {
        Pattern pattern = Pattern.compile(regex);

        return pattern.matcher(str).matches();
    }

    /**
     * Method isMatchCountRecord 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchCountRecord(String str, String message) {
        Pattern pattern = Pattern.compile("(?!0)\\d{1,8}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 格式不正确或者范围正确,范围为:[1-99999999]");
        }
    }

    /**
     * Method isMatchBankCardType 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchBankCardType(String str, String message) {
        Pattern pattern = Pattern.compile("0|1|2|[\\s]{0}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 只能为0、1或者2");
        }
    }

    /**
     * Method isMatchBankType 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchBankType(String str, String message) {
        Pattern pattern = Pattern.compile("[0-9]{7}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 只能为7位数字");
        }
    }

    /**
     * Method isMatchCustCertificationType 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchCustCertificationType(String str, String message) {
        Pattern pattern = Pattern.compile("[0-9A-Z]{1}|[\\s]{0}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 只能是[0-9]或者[A-Z]");
        }
    }

    /**
     * Method isMatchAmount 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchAmount(String str, String message) {
        Pattern pattern = Pattern.compile("(?!0)\\d{1,15}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message
                                               + "}[Assertion failed] - 格式不正确或者范围不正确,范围为:[1-999999999999999]分");
        }
    }

    /**
     * Method isMatchBankAcctType 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchBankAcctType(String str, String message) {
        Pattern pattern = Pattern.compile("1|2|[\\s]{0}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 只能是1或者2");
        }
    }

    /**
     * Method isMatchCardValidDate 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchCardValidDate(String str, String message) {
        Pattern pattern = Pattern.compile("([0][1-9]|[1][0-2])[0-9]{2}|[\\s]{0}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 信用卡有效期格式错误,格式为[月月年年]");
        }
    }

    /**
     * Method isMatchCardCvv2 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchCardCvv2(String str, String message) {
        Pattern pattern = Pattern.compile("[0-9]{3}|[\\s]{0}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 信用卡CVV2码格式不正确");
        }
    }

    /**
     * Method isMatchBankAccountNo 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchBankAccountNo(String str, String message) {
        Pattern pattern = Pattern.compile("[0-9-]{8,32}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 银行卡号须为8-32位的数字或者”-“组成");
        }
    }

    /**
     * Method isMatchStatus 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchStatus(String str, String message) {
        Pattern pattern = Pattern.compile("[1,2]{1}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 交易状态只能为1或者2");
        }
    }

    /**
     * Method isMatchCurrency 
     * Description 说明：
     *
     * @param str 类型：String
     * @param message 类型：String
     */
    public static void isMatchCurrency(String str, String message) {
        Pattern pattern = Pattern.compile("([CNY]|[USD]|[EUR]|[HKD]){3}");

        if (!pattern.matcher(str).matches()) {
            throw new IllegalArgumentException("{" + message + "}[Assertion failed] - 币种只支持[CNY,USD,EUR,HKD]");
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
